<?php

// @formatter:off
/**
 * @file WCFVPager.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: WCFVPager.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/includes/WCFVPager.php $
 */
// @formatter:on

class WCFVPager extends WCFactory {
	//
	// Public class methods.
	public static function GetAssigns($maxCount) {
		$out = array();

		if($maxCount > 1) {
			$out["pager_show"] = true;

			$page = self::GetPage();

			$first = 1;
			$previous = ($page == 1 ? $page : $page - 1);
			$next = ($page == $maxCount ? $page : $page + 1);
			$last = $maxCount;

			$uri = explode("?", $_SERVER["REQUEST_URI"]);
			$uriParams = isset($uri[1]) ? explode("&", $uri[1]) : array();

			foreach($uriParams as $position => $param) {
				if(preg_match("/^(page=([0-9]*)|page)$/", $param)) {
					unset($uriParams[$position]);
					continue;
				}
				if(preg_match("/^(resetcache=(.*)|resetcache|nocache=(.*)|nocache)$/", $param)) {
					unset($uriParams[$position]);
					continue;
				}
			}

			$link = "";
			$glue = "?";
			if(count($uriParams) > 0) {
				$link = "?".implode("&", $uriParams);
				$glue = "&";
			}
			$link .= $glue;

			$out["pager_first"] = "{$link}page={$first}";
			$out["pager_previous"] = "{$link}page={$previous}";
			$out["pager_current"] = $page;
			$out["pager_next"] = "{$link}page={$next}";
			$out["pager_last"] = "{$link}page={$last}";

			$out["pager_pages"] = array();
			$max = $page + 5 > $maxCount ? $maxCount : $page + 5;
			$min = $page - 5 < 1 ? 1 : $page - 5;
			for($i = $min; $i <= $max; $i++) {
				$out["pager_pages"][] = array(
					"name" => $i,
					"link" => "{$link}page={$i}"
				);
			}
			if($min != 1) {
				$out["pager_spacer_previous"] = true;
			}
			if($max != $maxCount) {
				$out["pager_spacer_next"] = true;
			}
		}

		return $out;
	}
	public static function GetAssignsIntoManager($HighestPage) {
		$manager = WCViewManager::I();
		foreach(self::GetAssigns($HighestPage) as $key => $value) {
			$manager->$key = $value;
		}
	}
	public static function GetAssignsIntoManagerFor($itemsCount, $limiter = "default") {
		$chunkSize = self::GetMaximumOf($limiter);
		self::GetAssignsIntoManager(ceil($itemsCount / $chunkSize));
	}
	public static function GetPage() {
		return isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	}
	public static function GetLimitsFor($key = "default") {
		$out = array(
			"min" => 1,
			"max" => 25,
			"page" => self::GetPage()
		);

		$chunkSize = self::GetMaximumOf($key);

		$out["min"] = (($out["page"] - 1) * $chunkSize) + 1;
		$out["max"] = $out["page"] * $chunkSize;

		return $out;
	}
	public static function GetMaximumOf($key = "default") {
		$chunkSize = 25;

		global $wcSkinConfig;

		if(isset($wcSkinConfig->max_per_page->{$key})) {
			$chunkSize = $wcSkinConfig->max_per_page->{$key};
		} elseif(isset($wcSkinConfig->max_per_page->default)) {
			$chunkSize = $wcSkinConfig->max_per_page->default;
		}

		return $chunkSize;
	}
}
